//-----------------------------------------------------------------------
// <copyright file="a_SeasonDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_SeasonDAOBase
    {
        #region Common methods
        public virtual a_Season Createa_SeasonFromReader(IDataReader reader)
        {
            a_Season item = new a_Season();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SeasonID"))) item.SeasonID = (int)reader["SeasonID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SeasonNameVN"))) item.SeasonNameVN = (string)reader["SeasonNameVN"];
                if (!reader.IsDBNull(reader.GetOrdinal("SeasonNameEN"))) item.SeasonNameEN = (string)reader["SeasonNameEN"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Season methods
            
        public virtual void Createa_Season(a_Season a_Season)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Seasons_ins");
                
                database.AddInParameter(dbCommand, "@SeasonNameVN", DbType.String, a_Season.SeasonNameVN);
                database.AddInParameter(dbCommand, "@SeasonNameEN", DbType.AnsiString, a_Season.SeasonNameEN);
                database.AddOutParameter(dbCommand, "@SeasonID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Season.SeasonID = (int)database.GetParameterValue(dbCommand, "@SeasonID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Season methods
        
        public virtual void Updatea_Season(a_Season a_Season)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Seasons_upd");            
                
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, a_Season.SeasonID);
                database.AddInParameter(dbCommand, "@SeasonNameVN", DbType.String, a_Season.SeasonNameVN);
                database.AddInParameter(dbCommand, "@SeasonNameEN", DbType.AnsiString, a_Season.SeasonNameEN);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Season methods
        public virtual void Deletea_Season(int seasonID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Seasons_del");
                
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, seasonID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Season methods
        
        public virtual a_Season Geta_Season(int seasonID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Seasons_sel");
                
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, seasonID);
                
                a_Season a_Season =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Season = Createa_SeasonFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Season;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_SeasonList methods
        public virtual a_SeasonCollection Geta_SeasonList(a_SeasonColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Seasons_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_SeasonCollection a_SeasonCollection = new a_SeasonCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Season a_Season = Createa_SeasonFromReader(reader);
                        a_SeasonCollection.Add(a_Season);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SeasonCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_SeasonCollection Geta_SeasonList(a_SeasonColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_SeasonList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}